#pragma once
#include <string>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <fstream>
#include <sys/fcntl.h>
#include <sys/wait.h>
#include <boost/algorithm/string.hpp>

namespace wjh_util
{
    std::string folder = "../temp/";

    class TimeUtil
    {
    public:
        //毫秒级时间戳
        static std::string getTimeMS()
        {
            struct timeval tv;
            gettimeofday(&tv, nullptr);
            return std::to_string(tv.tv_sec * 1000 + tv.tv_usec / 1000); 
        }
    };

    class Util
    {
    public:
        static std::string modify(std::string& FileName, const std::string& suffix)
        {
            std::string str = folder;
            str += FileName;
            str += suffix;
            return str;
        }

        //执行程序 ../temp/FileName.exe
        static std::string exe(std::string FileName)
        {
            return modify(FileName, ".exe");
        }

        //../temp/FileName.cpp
        static std::string src(std::string FileName)
        {
            return modify(FileName, ".cpp");
        }

        static std::string stdIn(std::string& FileName)
        {
            return modify(FileName, ".stdin");
        }

        static std::string stdOut(std::string& FileName)
        {
            return modify(FileName, ".stdout");
        }

        static std::string stdErr(std::string FileName)
        {
            return modify(FileName, ".stderr");
        }

        static std::string CompileErr(std::string FileName)
        {
            return modify(FileName, ".CompileErr");
        }

        static std::string run(std::string FileName)
        {
            return modify(FileName, ".exe");
        }

        static bool IsFileExits(std::string PathName)
        {
            struct stat st;
            if(stat(PathName.c_str(), &st) == 0) return true;

            return false;
        }
    };

    class FileUtil
    {
    public:
        static bool FileWrite(const std::string& code, const std::string& PathName)
        {
            std::ofstream in(PathName);
            if(!in.is_open())  return false;
            in.write(code.c_str(), code.size());
            in.close();
            return true;
        }

        static bool FileRead(const std::string& PathName, std::string& content, bool KeepNewline = true)
        {
            content.clear();
            std::ifstream out(PathName);
            if(!out.is_open()) return false;

            std::string line;
            while(std::getline(out, line))
            {
                content += line;
                content += KeepNewline == true? "\n" : ""; 
            }
            
            return true;
        }
    };

    class StringUtil
    {
    public:
        //切分字符串：sep 分隔符
        static void SplitStr(const std::string& input, std::vector<std::string>& output, const std::string& sep)
        {
            boost::split(output, input, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };
}

